<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="alert-filters">
  <div class="alert-filters__header">
    <div class="alert-filters__header__titles">
      <h3 class="alert-filters__header__title">Filters</h3>
      <div id="alertFiltersSubtitle" class="alert-filters__header__subtitle">Filters to apply condition only on a subset of events</div>
    </div>
    <button *ngIf="selectedRule != null" aria-label="Add filter" mat-stroked-button (click)="addControl()">
      <mat-icon svgIcon="gio:plus"></mat-icon>Add filter
    </button>
  </div>
  <ng-container *ngIf="selectedRule != null; else bannerBlock">
    <div *ngIf="form.controls?.length > 0">
      <!--
         The $any usage is a workaround to be able to use the FormArray directly without an unnecessary FormGroup
         see: https://github.com/angular/angular/issues/30264
      -->
      <form [formGroup]="$any(form)">
        <table class="gio-table-light alert-filters__table" aria-describedby="alertFiltersSubtitle">
          <thead [hidden]="true">
            <tr>
              <th>Filter</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <tr *ngFor="let control of form.controls; index as controlIndex" class="gio-form-listeners__table__row">
              <td class="gio-form-listeners__table__row__td">
                <metrics-simple-condition
                  [form]="$any(form.controls[controlIndex])"
                  [metrics]="metrics"
                  [referenceType]="referenceType"
                  [referenceId]="referenceId"
                ></metrics-simple-condition>
              </td>
              <td class="gio-form-listeners__table__row__td">
                <button type="button" mat-icon-button aria-label="Delete filter" (click)="removeControl(controlIndex)">
                  <mat-icon svgIcon="gio:cancel"></mat-icon>
                </button>
              </td>
            </tr>
          </tbody>
        </table>
      </form>
    </div>
  </ng-container>
  <ng-template #bannerBlock>
    <gio-banner-info>Select a rule before setting the filters.</gio-banner-info>
  </ng-template>
</div>
